<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .content {
            width: 300px;
            height: 300px;
            margin: 0 auto;
            text-align: center;
            font-size: 30px;
            background-color: whitesmoke;
        }
    </style>
</head>

<body>
    <div class="content">0</div>
    <script src="./js/lodash.min.js"></script>
    <script>
        // 1、使用lodash节流函数
        // const box=document.querySelector('.content')
        // let i=0
        // function addI(){
        //     box.innerHTML=i++
        // }
        // box.addEventListener('mousemove',_.throttle(addI,3000))

        // 2、封装函数（定时器）
        const box = document.querySelector('.content')
        let i = 0
        box.addEventListener('mousemove', throttle(addI, 3000))
        function addI() {
            i++
            box.innerHTML = i
        }
        function throttle(fn, t) {
            let timerId = null
            return function () {
                if (!timerId) {
                    timerId = setTimeout(function () {
                        fn()
                        timerId = null
                    }, t)
                }

            }


        }
    </script>

</body>

</html>